package com.young.nyd.service.impl;

import com.young.cms.model.AdministrativeRegion;
import com.young.cms.service.IAdministrativeRegionService;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.nyd.model.UviAgriculturallandinformation;
import com.young.nyd.model.UviCultivatechange;
import com.young.nyd.service.IUviAgriculturallandinformationService;
import com.young.nyd.service.IUviCultivatechangeService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 农用地信息表服务实现
* Created by  on 2018-11-1.
*/
@Service("uviAgriculturallandinformationService")
public class UviAgriculturallandinformationServiceImpl implements IUviAgriculturallandinformationService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务
    @Resource(name = "administrativeRegionService")//行政区划
    IAdministrativeRegionService administrativeRegionService;

    @Resource
    IUviCultivatechangeService iUviCultivatechangeService;

    private static final Logger logger = LoggerFactory.getLogger(UviAgriculturallandinformationServiceImpl.class);

    private static final String MAPPER = "com.young.nyd.mapper.uviAgriculturallandinformation";//mapper的namespace

    //获取数据列表
    @Override
    public List<UviAgriculturallandinformation> getList(UviAgriculturallandinformation obj) throws Exception {

        return dataAccessService.getList(MAPPER + ".getList", obj);
    }


    //根据省市县获取地块name
    @Override
    public List<UviAgriculturallandinformation> getLandByCounty(String county) throws Exception {
        UviAgriculturallandinformation obj = new UviAgriculturallandinformation();
        obj.setRegions(county);
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }


    //获取数据列表(分页)
    @Override
    public List<UviAgriculturallandinformation> getPage(UviAgriculturallandinformation obj, Page page) throws Exception {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public UviAgriculturallandinformation get(String id) throws Exception {
        UviAgriculturallandinformation rs = dataAccessService.getObject(MAPPER + ".get", id);
        if (rs != null){
            AdministrativeRegion region = administrativeRegionService.get(rs.getRegions());
            if (region != null){
                if (region.getAdminLevel() == 1){
                    rs.setProvinceCode(rs.getRegions());
                }else if(region.getAdminLevel() == 2){
                    rs.setCityCode(rs.getRegions());
                    rs.setProvinceCode(region.getParentId());
                }else if(region.getAdminLevel() == 3){
                    AdministrativeRegion cityRegion =  administrativeRegionService.get(region.getParentId());
                    rs.setCountyCode(region.getId());
                    rs.setCityCode(region.getParentId());
                    rs.setProvinceCode(cityRegion.getParentId());
                }
            }
        }
        /*dataAccessService.getObject(MAPPER + ".get", id)*/
        return rs;

    }

    //统计数量
    @Override
    public int count(UviAgriculturallandinformation obj) throws Exception {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(UviAgriculturallandinformation obj) throws Exception {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) throws Exception {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(UviAgriculturallandinformation obj) throws Exception {
        obj.setId(CommonUtil.getUUID());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<UviAgriculturallandinformation> list) throws Exception {
        int i = 0;
        for(UviAgriculturallandinformation item : list){
            i += this.insert(item);
        }
        return i;
    }

    //查询最大序号
    @Override
    public Integer maxSerialnumber(UviAgriculturallandinformation obj) throws Exception {
       Integer max = dataAccessService.getObject(MAPPER + ".maxSerialnumber",obj);
        return max;
    }
}